package com.meichis.yndms.ui.activity;

import android.view.View;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.meichis.mcsappframework.dialog.DropDownDialog;
import com.meichis.yndms.R;
import com.meichis.yndms.adapter.SalesRankStaffAdapter;
import com.meichis.yndms.databinding.ActivitySalesRankStaffBinding;
import com.meichis.yndms.ui.common.BaseActivity;
import com.meichis.yndms.vm.SalesRankStaffVM;

import java.util.ArrayList;

public class SalesRankStaffActivity extends BaseActivity {

    private ActivitySalesRankStaffBinding binding;
    private SalesRankStaffVM vm;
    private SalesRankStaffAdapter adapter;


    @Override
    protected void setDataBindingContentView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_sales_rank_staff);
        vm = new ViewModelProvider(this, new ViewModelProvider.NewInstanceFactory()).get(SalesRankStaffVM.class);
        binding.setVm(vm);
        binding.setClick(new ClickEvent());
        binding.setLifecycleOwner(this);
    }

    @Override
    protected void init() {
        binding.navigation.txtTitle.setText("人员排名");
        binding.navigation.navBack.setOnClickListener(v -> onBackPressed());
        addObserver();

        adapter = new SalesRankStaffAdapter(this, R.layout.item_sales_rank_staff, new ArrayList<>());
        binding.rc.setLayoutManager(new LinearLayoutManager(this));
        binding.rc.setAdapter(adapter);

        vm.GetUserSalesRank();
    }


    private void addObserver() {
        vm.getShowDialog().observe(this, aBoolean -> showLoading(aBoolean));

        vm.getSalesRankList().observe(this, salesRanks -> {
            if (salesRanks.size() == 0) {
                binding.llTitle.setVisibility(View.GONE);
            }else {
                binding.llTitle.setVisibility(View.VISIBLE);
            }
            binding.rc.setItemViewCacheSize(salesRanks.size());
            adapter.setList(salesRanks);
        });
    }


    public class ClickEvent {

        public void onSelectDateType() {
            new DropDownDialog<>(SalesRankStaffActivity.this, R.color.theme, "时间维度", "Name", vm.getDataTableList(), new DropDownDialog.SelectedListener() {
                @Override
                public void setSelect(Object o, int position) {
                    vm.getDataTable().setValue(vm.getDataTableList().get(position));
                    vm.GetUserSalesRank();
                }
            }).show();
        }
    }
}
